
package DAO;

import DTO.HLV;
import DTO.NGUOI;
import java.sql.Connection;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

public class HLVDAO {
     Connect con= new Connect();
     Connection cont;
     public HLVDAO()
     {
         cont=con.Connect("sa","123456"); 
     }
     public ResultSet Getdata()
     {
        String sqlString="Select * from VIEW_HLV";
        return con.GetdataTable(sqlString);
     }
     
     public ResultsModel Getdata1()
     {
         String[] title = {"Mã HLV","Họ Tên","Ngày Sinh","Quốc Tịch"};
         Result rs = new Result();
         return rs.GetResult(cont,"select * from VIEW_HLV", title);
     }
       public String GetMaFTenHLV(NGUOI n)
      {
          return con.GetMaDK("MA_BHL", "view_HLV", "HO_TEN",n.getHoten());
      }
      public void Insert(HLV hlv)
      {
          String sql  = "{call spThemHLV(?,?,?,?,?)}";
          ArrayList<String> a = new ArrayList();
          a.add("ThongBao");
          a.add(hlv.getNguoi().getManguoi());
          a.add(hlv.getNguoi().getHoten());
          SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
          String formattedDate = formatter.format(hlv.getNguoi().getDate()); 
          a.add(formattedDate);
          a.add(hlv.getNguoi().getQuoctich());
          con.Insert(sql, a, true);//ngay cho nay
      }
      
      public void Update(HLV hlv)
      {
          String sql = "{call spSuaHLV(?,?,?,?)}";
          ArrayList<String> a = new ArrayList();
          a.add("con heo");
          a.add(hlv.getNguoi().getManguoi());
          a.add(hlv.getNguoi().getHoten());
          SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
          String formattedDate = formatter.format(hlv.getNguoi().getDate()); 
          a.add(formattedDate);
          a.add(hlv.getNguoi().getQuoctich());
          
          con.Update(sql, a, true);
          
      }
      
      public void Delete(String MaHLV)
      {
          String sql = "{call spXoaHLV(?)}";
          ArrayList<String> a = new ArrayList();
          a.add("Thong bao");
          a.add(MaHLV);
          con.Update(sql,a, true);
      }
      
      public String LayMaCuoi()
      {
          String str="";
          try
          {
          String query = "select MA_BHL from HLV order by MA_BHL desc";
          ResultSet rs = null;
          rs = con.GetdataTable(query);
          rs.next();
          str = rs.getString(1);
          }
          catch(Exception ex)
          {}
          return str;
      }
}
